package com.ruoyi.media.mediaAccount.mapper;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.media.accountGroup.domain.MediaAccountGroup;
import com.ruoyi.media.echatsData.entity.PlatformAccount;
import com.ruoyi.media.echatsData.entity.ResData;
import com.ruoyi.media.mediaAccount.domain.MediaAccount;
import com.ruoyi.media.mediaAccount.domain.vo.MediaAccountVo;
import com.ruoyi.common.core.mapper.BaseMapperPlus;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * 自媒体账号列Mapper接口
 *
 * @author dqw
 * @date 2023-03-23
 */
public interface MediaAccountMapper extends BaseMapperPlus<MediaAccountMapper, MediaAccount, MediaAccountVo> {



    /**
     * 根据条件查询数据库是否已存在该账号
     * @return
     */
    @Select("SELECT id FROM `media_account` WHERE create_by = #{creatBy}  " +
        "AND  system_uniq_id = #{systemUniq} AND del_flag = 0")
    String selectAccountByUser(@Param("creatBy")String creatBy,
                               @Param("systemUniq")String systemUniq);


    /**
     * 获取视频号需要保持登录的账号
     * @return
     */
    @Select("SELECT * FROM `media_account` WHERE login_status = 1 " +
        "AND platform_code = 'wxsph' AND del_flag = 0  GROUP BY scan_random ")
    List<MediaAccount> keepLoginAccount();


    /**
     * 根据账号随机扫码参数修改登录状态
     * @return
     */
    @Update("UPDATE media_account SET login_status = 0 " +
        "WHERE scan_random = #{scanRandom}")
    int updateLoginStatusByScanRandom(@Param("scanRandom")String scanRandom);


    /**
     * 根据当前登录账号获取对应平台账号数
     * @return
     */
    @Select("SELECT p.platform_name `name`, COUNT(1) `value`   " +
        "FROM `media_account` a, `media_platform` p WHERE a.create_by = #{username} AND " +
        "p.platform_code = a.platform_code GROUP BY a.platform_code")
    List<ResData> getPlatformAccountCount(@Param("username")String username);


    /**
     * 根据用户id获取下级用户数据
     * @return
     */
    @Select("SELECT * FROM sys_user WHERE user_id = #{id}")
    SysUser getLowerUser(@Param("id")String id);


}
